package com.swss.g.user.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import com.swss.g.base.impl.BaseServiceImpl;
import com.swss.g.dept.bean.Dept;
import com.swss.g.user.bean.Module;
import com.swss.g.user.bean.Popedom;
import com.swss.g.user.bean.Role;
import com.swss.g.user.bean.RoleUser;
import com.swss.g.user.bean.User;
import com.swss.g.user.dao.ModuleDAO;
import com.swss.g.user.dao.RoleDAO;
import com.swss.g.user.dao.RoleUserDAO;
import com.swss.g.user.dao.UserDAO;
import com.swss.g.user.service.UserService;
import com.swss.util.HqlUtil;
import com.swss.util.Pagination;

public class UserServiceImpl extends BaseServiceImpl<User> implements
		UserService {
	private UserDAO userDao;
	private ModuleDAO moduleDao;

	private RoleUserDAO roleUserDao;

	private RoleDAO roleDao;

	public UserDAO getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDAO userDao) {
		this.setBaseDao(userDao);
		this.userDao = userDao;
	}

	public void saveRoleUser(RoleUser roleUser) throws Exception {
		// TODO Auto-generated method stub

		this.roleUserDao.saveOrUpdate(roleUser);
	}

	public RoleUserDAO getRoleUserDao() {
		return roleUserDao;
	}

	public void setRoleUserDao(RoleUserDAO roleUserDao) {
		this.roleUserDao = roleUserDao;
	}

	public List<RoleUser> findRoleUser(RoleUser roleUser) throws Exception {
		// TODO Auto-generated method stub
		Pagination pagination = new Pagination();
		pagination.setUsepage(false);
		return this.getRoleUserDao().findByExample(roleUser, pagination);
	}

	public RoleUser findOneRoleUser(Integer ruId) throws Exception {
		// TODO Auto-generated method stub
		return this.getRoleUserDao().get(ruId);
	}

	public void updateRoleUser(RoleUser roleUser) throws Exception {
		// TODO Auto-generated method stub

		this.roleUserDao.update(roleUser);
	}

	public Role findRoleByUserId(Integer userId) throws Exception {
		// TODO Auto-generated method stub

		return this.roleDao
				.findOneByHql("SELECT r FROM Role r,RoleUser ru WHERE ru.roleId=r.id AND ru.userId="
						+ userId);
	}

	public RoleUser findRoleUserById(Integer roleUserId) throws Exception {
		return this.getRoleUserDao().get(roleUserId);
	}

	public Pagination<User> findAllUser(Pagination pagination) throws Exception {
		// TODO Auto-generated method stub

		if (pagination == null) {
			pagination = new Pagination();
			pagination.setPagesize(2);
		}

		List list = this
				.getUserDao()
				.findByHql(
						"FROM User u,RoleUser ru,Role r,Dept d WHERE u.id=ru.userId and ru.roleId=r.id and u.deptNo=d.deptNo ORDER BY u."
								+ pagination.getOrderBy()
								+ " "
								+ pagination.getOrderType(), pagination);

		List<User> userList = new ArrayList();

		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			User user = (User) obj[0];

			user.setRole((Role) obj[2]);
			user.setDept((Dept) obj[3]);
			userList.add(user);
		}
		pagination.setResult(userList);

		return pagination;
	}

	/**
	 * 查询用户的所有权限
	 */
	@SuppressWarnings("unchecked")
	public List<Module> findModulePopesByUserId(Integer userId)
			throws Exception {
		// TODO Auto-generated method stub
		
		Pagination pagination = new Pagination();
		pagination.setUsepage(false);
		pagination.setOrderBy("queue");
		pagination.setOrderType("asc");
		
		List list = this.userDao
				.findByHql(
						"SELECT p FROM User u,RoleUser ru,Role r,RolePopedom rp,Popedom p  WHERE u.id="
								+ userId
								+ " AND u.id=ru.userId AND ru.roleId=r.id AND r.id=rp.roleId AND rp.popedomId=p.id ORDER BY p.queue ASC",
						pagination);

		


		
		return list;
		
		//return null;
	}

	public ModuleDAO getModuleDao() {
		return moduleDao;
	}

	public void setModuleDao(ModuleDAO moduleDao) {
		this.moduleDao = moduleDao;
	}

	public RoleDAO getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDAO roleDao) {
		this.roleDao = roleDao;
	}

	public User login(User user) throws Exception {
		// TODO Auto-generated method stub

		return this.findOneByExample(user);
	}

	public Pagination<User> findAllUser(Pagination pagination, User user)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Pagination<User> findUserByLike(Pagination pagination, User user,
			String deptNo) throws Exception {

		if (pagination == null) {
			pagination = new Pagination();

		}

		String hql = "FROM User u,RoleUser ru,Role r,Dept d WHERE u.id=ru.userId AND ru.roleId=r.id AND u.deptNo=d.deptNo";
		if (!deptNo.trim().equals("00")) {
			hql += " AND u.deptNo LIKE '" + deptNo.trim() + "%'";
		}

		// TODO Auto-generated method stub
		hql += HqlUtil.bean2HqlLike(user, "u") ;
		List list = this.getUserDao().findByHql(hql+ " ORDER BY u."
				+ pagination.getOrderBy() + " " + pagination.getOrderType(), pagination);

		List<User> userList = new ArrayList();

		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			User u = (User) obj[0];
			u.setRole((Role) obj[2]);
			u.setDept((Dept) obj[3]);
			userList.add(u);
		}
		pagination.setResult(userList);

		pagination.setRowcount(this.userDao.findCount(hql));
	
		return pagination;
	}

}
